 /*
 * Copyright (C) 2017-2024 Alibaba Group Holding Limited
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _BLE_AIS_CLIENT_H
#define _BLE_AIS_CLIENT_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "ais_ota.h"
#include "aos/kernel.h"
#include "timer_port.h"

#define OTA_CRYPTO_UNIT_SIZE (16)

#ifndef CONFIG_AIS_TOTAL_FRAME
#define CONFIG_AIS_TOTAL_FRAME 16
#endif

#ifndef CONFIG_OTA_PAYLOAD_MAX
#ifndef CONFIG_BT_L2CAP_RX_MTU
#define CONFIG_BT_L2CAP_RX_MTU 251
#endif
#define CONFIG_OTA_PAYLOAD_MAX (CONFIG_BT_L2CAP_RX_MTU - 3)
#endif

#define AIS_DISCONNECT_TIMEOUT 500 // ms
#define OTA_RECV_BUF_SIZE      (CONFIG_AIS_TOTAL_FRAME * CONFIG_OTA_PAYLOAD_MAX)
#define OTA_RECV_MAX_ERR_COUNT (CONFIG_AIS_TOTAL_FRAME * 3)
#define BLE_OTA_IMAGE_ID_MAGIC (0xA5A5A5)
#define BLE_OTA_IMAGE_ID_A     (BLE_OTA_IMAGE_ID_MAGIC << 8 | DFU_IMAGE_A)
#define BLE_OTA_IMAGE_ID_B     (BLE_OTA_IMAGE_ID_MAGIC << 8 | DFU_IMAGE_B)

#define AIS_OTA_AUTH_TIMEOUT    10000 // 10s
#define AIS_OTA_DISCONN_TIMEOUT 60000 // 60s
#define AIS_OTA_REPORT_TIMEOUT  (CONFIG_AIS_TOTAL_FRAME * 400)
#define AIS_OTA_REBOOT_TIMEOUT  800 // 800ms

    // For pingpong OTA
    enum
    {
        DFU_IMAGE_A = 0,
        DFU_IMAGE_B,
        DFU_IMAGE_TOTAL = DFU_IMAGE_B,
        DFU_IMAGE_INVALID_ID,
    };

    typedef struct ais_ota_ctx_s {
        uint8_t  init_flag;
        uint8_t  state;
        uint8_t  channel;
        uint8_t  err_count;
        uint8_t  last_seq;
        uint8_t  total_frame;
        uint8_t  except_seq;
        uint32_t rx_size;
        uint8_t  image_type;
        uint32_t image_ver;
        uint32_t image_size;
        uint16_t image_crc16;
        uint8_t  crc_check_flag;
        uint8_t  ota_flag;
        uint8_t  auto_reboot_flag;

        uint8_t     flash_clean : 1;
        uint8_t     ota_ready   : 1;
        uint16_t    rx_len;
        uint8_t     recv_buf[OTA_RECV_BUF_SIZE];
        ota_timer_t state_update_timer;
        ota_timer_t disconnect_timer;
    } ais_ota_ctx_t;

    int  ais_ota_client_init();
    void ais_ota_crc_check_ctl(uint8_t flag);
    void ais_ota_auto_reboot_ctl(uint8_t flag);

#ifdef __cplusplus
}
#endif

#endif
